package com.czk.classreader.complextransport;

import com.czk.utils.FileUtil;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/8/30 17:06
 */
public class MethodRemoveGetFieldPutFieldCore {
    public static void main(String[] args) {
        String relative_path = "F:\\IdeaProjects\\asm\\target\\classes\\com\\czk\\sample\\HelloWorld.class";
        byte[] bytes1 = FileUtil.readBytes(relative_path);

        //（1）构建ClassReader
        ClassReader cr = new ClassReader(bytes1);

        //（2）构建ClassWriter
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);

        //（3）串连ClassVisitor
        int api = Opcodes.ASM9;
        ClassVisitor cv = new MethodRemoveGetFieldPutFieldVisitor(api, cw);
        ClassVisitor cv1=new MethodRemoveAddZeroVisitor(api,cv);
        ClassVisitor cv2=cv1;

        //（4）结合ClassReader和ClassVisitor
        int parsingOptions = ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
        cr.accept(cv2, parsingOptions);

        //（5）生成byte[]
        byte[] bytes2 = cw.toByteArray();

        FileUtil.writeBytes(relative_path, bytes2);
    }
}
